/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.wpi.first.wpilibj.templates.commands;

import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;

/**
 *
 * @author Team 3502
 */
public class DriveToTarget extends CommandBase {
    private double targetX;
    private boolean aligned;
    private Timer timer = new Timer();

    public DriveToTarget() {
        // Use requires() here to declare subsystem dependencies
        // eg. requires(chassis);
        requires(drive);
    }

    // Called just before this Command runs the first time
    protected void initialize() {
        aligned = false;
        //drive.takesnapshot();
        targetX = drive.findtarget(); //the initial x location of the highest hoop
        System.out.print("DriveToTarget init() targetX = " + targetX);
        //drive.initsensors();
        //timer.start();
        //timer.reset();
    }

    // Called repeatedly when this Command is scheduled to run
    protected void execute() {
        //System.out.println("Loop Start Time = " + timer.get());
        //drive.takesnapshot();
        targetX = drive.checktarget();
        System.out.print("DriveToTarget execute() targetX = " + targetX);
        if (targetX < -0.08){
            System.out.println("Target is to the left, turn CCW");
            drive.driveWithJoy(0, -0.6);
        }
        else if (targetX > 0.08){
            System.out.println("Target is to the right, turn CW");
            drive.driveWithJoy(0, 0.6);
        }
        else {
            drive.driveWithJoy(0, 0);
            //aligned = true; //comment this out if you don't want the command to end when testing
            System.out.println("Robot Aligned!!");
        }
    }

    // Make this return true when this Command no longer needs to run execute()
    protected boolean isFinished() {
        if (aligned == true){
            return true;
        }
        else {
            return false;
        }
    }

    // Called once after isFinished returns true
    protected void end() {
    }

    // Called when another command which requires one or more of the same
    // subsystems is scheduled to run
    protected void interrupted() {
    }

}